package com.liwei.web.listener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Created by Liwei on 2015/11/1.
 */
public class SpringWebServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("容器启动的时候监听的方法。。。");
        // 1、 获取 Spring 配置文件的名称
        ServletContext servletContext =servletContextEvent.getServletContext();
        String configLocation =servletContext.getInitParameter("configLocation");
        System.out.println("configLocation " + configLocation);
        // 2、创建 IOC 容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
        // 3、把 IOC 容器放到 ServletContext 的一个属性中
        servletContext.setAttribute("ApplicationContext",ctx);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("容器销毁的时候监听的方法。。。");
    }
}
